package org.adorsys.aderp.orga.service;

import java.util.List;

import org.adorsys.aderp.orga.domain.OrgaPerson;
import org.adorsys.aderp.orga.domain.OrgaRelation;
import org.adorsys.aderp.orga.domain.OrgaRelationRole;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;


public class OrgaRelationServiceImpl implements OrgaRelationService {

	public List<OrgaRelation> findByPerson(OrgaPerson person){
		return orgaRelationRepository.findByPerson(person);
	}
	public List<OrgaRelation> findByTargetPerson(OrgaPerson person){
		return orgaRelationRepository.findByTargetPerson(person);
	}

	public Page<OrgaRelation> findByPersonPage(OrgaPerson person,Integer page, Integer size){
		return orgaRelationRepository.findByPerson(person, new PageRequest(page, size));
	}
	public Page<OrgaRelation> findByTargetPersonPage(OrgaPerson person,Integer page, Integer size){
		return orgaRelationRepository.findByTargetPerson(person, new PageRequest(page, size));
	}
	public Page<OrgaRelation> searchOrgaRelations(OrgaPerson person, OrgaPerson targetPerson, 
			OrgaRelationRole relationRole,Integer page, Integer size){
		PageRequest pageable = new PageRequest(page, size);
        if(person!=null && targetPerson==null && relationRole==null){
        	return orgaRelationRepository.findByPerson(person, pageable);
        } else if (person!=null && targetPerson!=null && relationRole==null){
        	return orgaRelationRepository.findByPersonAndTargetPerson(person, targetPerson, pageable);
        } else if (person!=null && targetPerson!=null && relationRole!=null){
        	return orgaRelationRepository.findByPersonAndTargetPersonAndRelationRole(person, targetPerson, relationRole, pageable);
        } else if (person==null && targetPerson!=null && relationRole==null){
        	return orgaRelationRepository.findByTargetPerson(targetPerson, pageable);
        } else if (person==null && targetPerson!=null && relationRole!=null){
        	return orgaRelationRepository.findByTargetPersonAndRelationRole(targetPerson, relationRole, pageable);
        } else if (person==null && targetPerson!=null && relationRole!=null){
        	return orgaRelationRepository.findByRelationRole(relationRole, pageable);
        } else {
        	return orgaRelationRepository.findAll(pageable);
        }
	}
}
